www.gusucode.com > VC++ 动画系统托盘图标的显示源码程序 > VC++ 动画系统托盘图标的显示源码程序/code/Taskdemo/ADGateDlg.cpp

    //Download by http://www.NewXing.com
// ADGateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ADGate.h"
#include "ADGateDlg.h"
#include "AboutDlg.h"
#include "GHeader.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CADGateDlg dialog

CADGateDlg::CADGateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CADGateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CADGateDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CADGateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADGateDlg)
	DDX_Control(pDX, IDC_LIST1, m_all);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADGateDlg, CDialog)
	//{{AFX_MSG_MAP(CADGateDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_COMMAND(ID_EASYGPS_ABOUT, OnEasygpsAbout)
	ON_COMMAND(ID_ICON_HIDE, OnIconHide)
	ON_COMMAND(ID_ICON_SHOW, OnIconShow)
	ON_COMMAND(ID_SYSTEMSETUP, OnSystemsetup)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADGateDlg message handlers

BOOL CADGateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

#ifdef CURRENCYVER
	SetWindowText(_L("\"神州通\"E@syGPS车辆监控管理系统 V2.0 自动拨号服务器"));
#else
	SetWindowText(_L("\"神州通\"现场查勘管理系统 V2.0 自动拨号服务器"));
#endif

	//显示本窗口:为了能够显示加载的初始化信息
	ShowWindow (SW_MAXIMIZE);

	CreateLog ();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CComQIPtr<ISkin> pSkin = GetDlgItem(IDC_SKIN1)->GetControlUnknown();
	pSkin->ApplySkin((long)m_hWnd);

	//设置列表头和显示属性:行选定,热键跟随,具有网格
	m_all.SetExtendedStyle (m_all.GetExtendedStyle ()  | LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_TRACKSELECT | LVS_EX_GRIDLINES | LVS_EX_INFOTIP );
	//初始化列表控件的列:
	int colNum=0;
	m_all.InsertColumn(colNum++,TEXT("序号"),LVCFMT_LEFT,50 );
	m_all.InsertColumn(colNum++,TEXT("消息摘要"),LVCFMT_LEFT,70,-1);
	m_all.InsertColumn(colNum++,TEXT("发生时间"),LVCFMT_LEFT,130,-1);
	m_all.InsertColumn(colNum++,TEXT("消息来源"),LVCFMT_LEFT,100,-1);
	m_all.InsertColumn(colNum++,TEXT("详细说明"),LVCFMT_LEFT,500,-1);
	
	//初始化位置
	ShowInformation ();
//LVM_GETHEADER
	//可以显示信息
	m_bViewStatus=TRUE;


	//启动任务兰
	m_taskbar.RWESInitData ();

	//启动数据库
	//获取用户名和密码
	wchar_t username[64] = _T(""),userpwd[64] = _T("");
	if(!loginfoini.GetStringFromINI (_T("DBUN"),username))
	{
		FillListToShow(SHOW_SELF,_T("配置文件"),_T("×配置文件被恶意修改"));
		return FALSE;
	}
	if(!loginfoini.GetStringFromINI (_T("DBPWD"),userpwd))
	{
		FillListToShow(SHOW_SELF,_T("配置文件"),_T("×配置文件被恶意修改"));
		return FALSE;
	}
	USES_CONVERSION;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CADGateDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CADGateDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CADGateDlg::CreateLog()
{
	wchar_t file[MAX_PATH];
	wsprintf(file,TEXT("\\%d-%02d-%02d-%02d-%02d-%02d.gps\0"),COleDateTime::GetCurrentTime().GetYear(),
		COleDateTime::GetCurrentTime().GetMonth(),COleDateTime::GetCurrentTime().GetDay(),
		COleDateTime::GetCurrentTime().GetHour(),COleDateTime::GetCurrentTime().GetMinute(),
		COleDateTime::GetCurrentTime().GetSecond());
	wchar_t  tmp[MAX_PATH]=TEXT("\0");
	if(GetCurrentDirectory (MAX_PATH,tmp)==0)
		return;
	wcscat(tmp,TEXT("\\log\\"));
	wcscat(tmp,file);

	m_log.Open(tmp,CFile::modeCreate|CFile::modeReadWrite);
}
BOOL CALLBACK EnumProc( HWND hWnd, LPARAM lParam)
{
	static const TCHAR szAfxOldWndProc[] = _T("AfxOldWndProc423");  // Visual C++ 6.0
	//check for property and unsubclass if necessary
	WNDPROC oldWndProc = (WNDPROC)::GetProp(hWnd, szAfxOldWndProc);
	if (oldWndProc!=NULL)
	{
		SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
		RemoveProp(hWnd, szAfxOldWndProc);
	}
	
	return TRUE;
}




LRESULT CADGateDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	//本程序还增加了系统托盘功能,对于系统托盘,本程序处理了两个特殊情况,
	//一个是系统托盘弹出的菜单在失去焦点时会自动消失,
	//另一个是在Explorer.exe崩溃重建时系统托盘也自动重建。
	static UINT const WM_TASKBARCREATED = RegisterWindowMessage( _T("TaskbarCreated") );
	if( WM_TASKBARCREATED == message ) // Explorer.exe重新启动
	{
		m_taskbar.RWESInitData ();
		return TRUE;
	}//

	// TODO: Add your specialized code here and/or call the base class
	switch( message )
	{
	case WM_ENDSESSION:
		{
		//对话框多处理了一个WM_ENDSESSION消息,否则用户在注销并重新登陆之后,
		//程序将失去响应
		DWORD dwProcessId;
	    DWORD dwThreadId= GetWindowThreadProcessId(m_hWnd,&dwProcessId);
		EnumThreadWindows(dwProcessId, EnumProc,(LPARAM) dwThreadId);
		}
		return TRUE;
	case WM_CLOSE:
		ShowWindow(SW_HIDE);
		return TRUE;
	case WM_QUERYENDSESSION:
		ShowWindow(SW_HIDE);
		break;
	case WM_SYSCOMMAND:
		switch( wParam )
		{
		// 隐藏
		case SC_MINIMIZE:
		case SC_CLOSE:
			ShowWindow(SW_HIDE);
			return TRUE;
		case SC_MAXIMIZE:
		case SC_SIZE:
		case  SC_RESTORE:
			m_all.SetSelectionMark(m_all.GetItemCount());
			m_all.EnsureVisible(m_all.GetItemCount(),TRUE);
			break;
		}
		break;
	case WM_CISTATUS:
		switch( lParam )
		{
		case WM_LBUTTONDBLCLK:	// 恢复显示
			if( IsWindowVisible() )
			{
				ShowWindow(SW_HIDE);
			}
			else
			{
				ShowWindow(SW_SHOWMAXIMIZED);
				SetForegroundWindow();
				m_all.SetSelectionMark(m_all.GetItemCount());
				m_all.EnsureVisible(m_all.GetItemCount(),TRUE);
			}
			return TRUE;
		case WM_RBUTTONUP:		// 显示菜单
			POINT point;
			HMENU hMenu, hSubMenu;
			GetCursorPos(&point);
			hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_TASKBARMENU));
			hSubMenu = GetSubMenu(hMenu, 0);
			SetForegroundWindow();
			SetMenuDefaultItem(hSubMenu, ID_ICON_SHOW, FALSE);
			TrackPopupMenu(hSubMenu,TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_LEFTALIGN,point.x, point.y, 0, this->GetSafeHwnd(), NULL);
			PostMessage(WM_NULL,0,0);
			DestroyMenu(hMenu);
			return TRUE;
		}
		return TRUE;
	}


	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CADGateDlg::Destroy()
{

}

void CADGateDlg::FillListToShow(int iType, wchar_t strAction[], wchar_t strRemark[])
{
	//if(!IsWindowVisible())
	//	return;
	wchar_t strType[1024]=TEXT("\0"),strTime[1024]=TEXT("\0");//类型和时间定义和暂存数据
	//类型分辨
	switch(iType)
	{
	case SHOW_MAS:
		wcscpy(strType,TEXT("监控代理"));
		break;
	case SHOW_DIAL:
		wcscpy(strType,TEXT("拨号消息"));
		break;
	case SHOW_CLIENT:
		wcscpy(strType,TEXT("系统管理"));
		break;
	case SHOW_SELF:
		wcscpy(strType,TEXT("系统消息"));
		break;
	default:
		wcscpy(strType,TEXT("异常消息"));
		break;
	}
	
	//时间字符化
	wsprintf(strTime,TEXT("%d-%02d-%02d %02d:%02d:%02d\0"),COleDateTime::GetCurrentTime().GetYear(),
		COleDateTime::GetCurrentTime().GetMonth(),COleDateTime::GetCurrentTime().GetDay(),
		COleDateTime::GetCurrentTime().GetHour(),COleDateTime::GetCurrentTime().GetMinute(),
		COleDateTime::GetCurrentTime().GetSecond());

	//填充全部显示控件部分
	int colNum=1,iIndex=0;
	wchar_t strTmp[1024]=TEXT("\0");
	iIndex=m_all.GetItemCount() +1;
	if(iIndex>99999)
	{
		m_all.DeleteAllItems ();
		iIndex=1;
		
		m_log.Close();
		CreateLog ();
	}
	wchar_t sbuf[2048]=TEXT("\0");
	wsprintf(sbuf,TEXT("ID=%06d  time=%s  type=%20s  Action=%10s  remark=%s\r\n\0"),
		iIndex,strTime,strType,strAction,strRemark);
	char WriteBuf[1024]="\0";
	WideCharToMultiByte (CP_ACP,0,sbuf,-1,WriteBuf,256,NULL,NULL); 
	m_log.Write(WriteBuf,strlen(WriteBuf));

	//发送到监控客户处
	if (!m_bViewStatus )
		return ;

	wsprintf(strTmp,TEXT("%ld"),iIndex);
	iIndex=m_all.InsertItem(iIndex,strTmp);//序号
	m_all.SetItemText(iIndex,colNum++,strAction);//动作
	m_all.SetItemText(iIndex,colNum++,strTime);//时间
	m_all.SetItemText(iIndex,colNum++,strType);//类型
	m_all.SetItemText(iIndex,colNum++,strRemark);//说明
	m_all.SetSelectionMark(iIndex);
	m_all.EnsureVisible(iIndex,TRUE);
}

void CADGateDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	//m_taskbar.pIconData.uFlags = NIF_ICON;
	//m_taskbar.pIconData.hIcon = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOPPING), IMAGE_ICON, 16, 16, 0);
	//Shell_NotifyIcon( NIM_MODIFY, &m_taskbar.pIconData );		// 更改系统托盘图标
	
	/*theApp.m_bServiceRun=FALSE;
	KillTimer (m_taskbar.m_Timer);
	devForm.DoExit ();
	dbsql.DoExit ();
	MessageBox(_L("CADGateDlg::OnDestroy"));
	Shell_NotifyIcon(NIM_DELETE, &m_taskbar.pIconData);	*/	// 删除from系统托盘
	
}

void CADGateDlg::OnEasygpsAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

void CADGateDlg::OnIconHide() 
{
	// TODO: Add your command handler code here
	if( IsWindowVisible() )
	{
		ShowWindow(SW_HIDE);
	}	
}

void CADGateDlg::OnIconShow() 
{
	if( !IsWindowVisible() )
	{
		ShowWindow(SW_SHOWMAXIMIZED);
		SetForegroundWindow();
		m_all.SetSelectionMark(m_all.GetItemCount());
		m_all.EnsureVisible(m_all.GetItemCount(),TRUE);
	}
}

void CADGateDlg::OnSystemsetup() 
{
	// TODO: Add your command handler code here
}

void CADGateDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(IsWindowVisible())
	{
		m_all.SetSelectionMark(m_all.GetItemCount());
		m_all.EnsureVisible(m_all.GetItemCount(),TRUE);
		ShowInformation();
	}
}

BOOL CADGateDlg::PreTranslateMessage(MSG* pMsg) 
{
	if(WM_KEYDOWN == pMsg->message)
		return TRUE;

	return CDialog::PreTranslateMessage(pMsg);
}

void CADGateDlg::ShowInformation()
{
	CRect viewRc,ctrlRc;
	GetClientRect (&viewRc);
	viewRc.left +=1;
	viewRc.right =viewRc.right -1;
	int colNum=0;
	int iWidth=50;
	int iTemp=0;
	//序号
	m_all.SetColumnWidth(colNum++,iWidth);
	//消息摘要
	m_all.SetColumnWidth(colNum++,80);
	iWidth+=80;
	//时间
	iTemp=int(viewRc.Width()*0.2)>130?130:int(viewRc.Width()*0.2);
	m_all.SetColumnWidth(colNum++,iTemp);
	iWidth+=iTemp;
	//消息来源
	iTemp=int(viewRc.Width()*0.2)>100?100:int(viewRc.Width()*0.2);
	m_all.SetColumnWidth(colNum++,iTemp);
	iWidth+=iTemp;
	m_all.SetColumnWidth(colNum++,viewRc.Width()-iWidth-20);

	m_all.MoveWindow (&viewRc);
}